<?php

//  empty_collection.php
//  Memento 3.0
//
//  Created by Christopher Burrows on 10-08-01
//  Copyright 2010 Team Memento. All rights reserved.
//
//    Description:
//  Clears out the backup collection matching
//  collection_id, which is retrieved from the passed
//  session_token.  This is called when replacing a
//  collection with new backup data.
//
//  10-08-02: File created. (C. Maddocks)
//
//  HTML POST Fields:
//  'session_token' (string)
//
//  OUTPUT:
//  Line 1: False if validation fails
//  Line 2: Error message (if available)

include ($_SERVER['DOCUMENT_ROOT'].'/functions.php');
connect_to_database();

$session_token = $_REQUEST ['session_token'];

if (!validate_session_token($session_token)) {

    echo '0'.PHP_EOL;

} else {

    // Get the collection_id
    $result = mysql_query("SELECT * FROM sessions WHERE
            session_token='$session_token'");
    $row = mysql_fetch_array($result);
    $collection_id = intval($row['collection_id']);
    
    // Get an array of memory_ids associated with that collection_id
    $sql = "DELETE FROM memories WHERE
            collection_id=$collection_id";
    $result = mysql_query($sql);
    
    if ($result) {
        echo '1'.PHP_EOL;  
    } else {
        echo '0'.PHP_EOL;
        echo mysql_error() . PHP_EOL;
    }  
        
}

?>